package com.ruoyi.canteen.service.impl;

import java.util.*;

import com.ruoyi.canteen.domain.CanteenCpClass;
import com.ruoyi.canteen.domain.CanteenCpSku;
import com.ruoyi.canteen.domain.CanteenCpSpecial;
import com.ruoyi.canteen.mapper.CanteenCpClassRelationMapper;
import com.ruoyi.canteen.vo.CanteenCpSkuVo;
import com.ruoyi.canteen.vo.CanteenCpVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.canteen.mapper.CanteenCpSpecialMapper;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.canteen.mapper.CanteenCpMapper;
import com.ruoyi.canteen.domain.CanteenCp;
import com.ruoyi.canteen.service.ICanteenCpService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 菜品Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-08
 */
@Service
public class CanteenCpServiceImpl implements ICanteenCpService 
{
    @Autowired
    private CanteenCpMapper canteenCpMapper;

    @Autowired
    private CanteenCpClassRelationMapper canteenCpClassRelationMapper;

    @Autowired
    private CanteenCpSpecialMapper canteenCpSpecialMapper;

    /**
     * 查询菜品
     * 
     * @param id 菜品主键
     * @return 菜品
     */
    @Override
    public CanteenCp selectCanteenCpById(Long id)
    {
        return canteenCpMapper.selectCanteenCpById(id);
    }



    @Override
    public List<CanteenCpClass> getCanteenCpClassById(Long id) {
        return canteenCpMapper.getCanteenCpClassById(id);
    }

    @Override
    public List<CanteenCpSpecial> getCanteenCpSpecialsById(Long id) {
        return canteenCpMapper.getCanteenCpSpecialsById(id);
    }

    /**
     * 查询菜品列表
     * 
     * @param canteenCp 菜品
     * @return 菜品
     */
    @Override
    @DataScope(deptAlias = "U")
    public List<CanteenCp> selectCanteenCpList(CanteenCp canteenCp)
    {
        //return canteenCpMapper.selectCanteenCpList2(canteenCp);
        return canteenCpMapper.selectCanteenCpList(canteenCp);
    }

    /**
     * 新增菜品
     * 
     * @param canteenCp 菜品
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCanteenCp(CanteenCp canteenCp) throws Exception
    {
        //插入菜品
        canteenCpMapper.insertCanteenCp(canteenCp);
       //插入菜品分类
        Map<String, Object> saveParams = new HashMap<>();
        saveParams.put("cId", canteenCp.getId());
        //这里指定两个角色的ID进行测试
        saveParams.put("classIds", Arrays.asList(canteenCp.getCpClass()) );
        insertCpClass(saveParams);
         //插入菜品规格价格
        for (CanteenCpSpecial s : canteenCp.getCanteenCpSpecialList()){
             s.setCpId(canteenCp.getId());
            canteenCpSpecialMapper.insertCanteenCpSpecial(s);
        }
        return 1;
    }

    @Override
    public int insertCpClass(Map<String, Object> map) {
        return canteenCpClassRelationMapper.insertCpClass(map);
    }

    /**
     * 修改菜品
     * 
     * @param canteenCp 菜品
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCanteenCp(CanteenCp canteenCp) throws Exception
    {
        canteenCpMapper.updateCanteenCp(canteenCp);
        //修改单品规格表
        CanteenCpSpecial special = null;
        for (int i = 0; i < canteenCp.getCanteenCpSpecialList().size(); i++) {
            special = canteenCp.getCanteenCpSpecialList().get(i);

            if(canteenCp.getIsSingleSpec() == 0)
                canteenCpSpecialMapper.updateCanteenCpSpecial(special);
        }



        //先删除关联表，再插入
        deleteCanteenCpClassRelationByCpId(canteenCp.getId());
        Map<String, Object> saveParams = new HashMap<>();
        saveParams.put("cId", canteenCp.getId());
        //这里指定两个角色的ID进行测试
        saveParams.put("classIds", Arrays.asList(canteenCp.getCpClass()) );
        return insertCpClass(saveParams);

    }

    /**
     * 批量删除菜品
     * 
     * @param ids 需要删除的菜品主键
     * @return 结果
     */
    @Override
    public int deleteCanteenCpByIds(Long[] ids)
    {
        return canteenCpMapper.deleteCanteenCpByIds(ids);
    }

    @Override
    public int deleteCanteenCpClassRelationByCpId(Long cpId) {
        return canteenCpClassRelationMapper.deleteCanteenCpClassRelationByCpId(cpId);
    }

    /**
     * 删除菜品信息
     * 
     * @param id 菜品主键
     * @return 结果
     */
    @Override
    public int deleteCanteenCpById(Long id)
    {
        return canteenCpMapper.deleteCanteenCpById(id);
    }

    /**
     * 查询菜品列表
     * @param cpClassId 菜品类型
     * @return
     */
    @Override
    public List<CanteenCp> selectCpListByClass(Integer cpClassId){
        List<CanteenCp> cpList = canteenCpMapper.selectCpListByClass(cpClassId);
        return cpList;
    }

    @Override
    public List<CanteenCpVo> selectCpVoListByClass(Integer cpClassId){
        List<CanteenCpVo> cpVos= canteenCpMapper.selectCpVoListByClass(cpClassId);
        return cpVos;
    }

    @Override
    public List<CanteenCpSku> selectCpSkuListByClass(Integer cpClassId){
        return canteenCpMapper.selectCpSkuListByClass(cpClassId);
    }

    /***
     * 查询当前菜品是否为同一门店
     * @param cpIds 菜品ID
     * @return
     */
    @Override
    public boolean isSameMd(String cpIds){
        Integer count = canteenCpMapper.selectMdCountByCps(cpIds);
        return (count == 1);
    }

}
